package jee.boot.module.bpm.validation;

import jee.boot.common.basic.ServiceException;
import jee.boot.common.basic.result.MapData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.Email;

/**
 * 非空校验
 */
public class PhoneValidation extends BasicValidation implements DataValidation {
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 电话号码正则表达式（支持手机号码，3-4位区号，7-8位直播号码，1－4位分机号）
     */
    private String pattern = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";

    /**
     * 数据校验
     *
     * @param value
     * @param params
     */
    @Override
    @Email
    public void validation(Object value, Object... params) {
        if (value == null) {
            return;
        }
        boolean matches = matches(value.toString(), pattern);
        if (!matches) {
            throw new ServiceException(1428, MapData.instance().put("param", getParam(0, "", params)).data());
        }

    }
}
